﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LongNet.Util
{
    
    public class StringUtil
    {
        public static string AreaStringFormat = "0.00";
        /// <summary>
        /// 小写金额转成大写
        /// </summary>
        /// <param name="Money">小写金额</param>
        /// <returns></returns>
        public static string MoneyToUp(double Money)
        {
            try
            {
                string s = Money.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");//d + "\n" +

                string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");

                return Regex.Replace(d, ".", delegate(Match m) { return "负圆空零壹贰叁肆伍陆柒捌玖空空空空空空空分角拾佰仟万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 小写金额转成大写
        /// </summary>
        /// <param name="Money">小写金额字符串</param>
        /// <returns></returns>
        public static string MoneyToUp(string Money)
        {
            try
            {
                double dMoney = double.Parse(Money);

                return MoneyToUp(dMoney);
            }
            catch
            {
                return "";
            } 
        }
        /// <summary>
        /// 将整形转换成中写汉字
        /// </summary>
        /// <param name="iNumber">数字</param>
        /// <returns></returns>
        public static string ChangeToMiddle(int iNumber)
        {
            try
            {
                if (iNumber == 0)
                {
                    return "0";
                }
                string s = iNumber.ToString("#L#E#D#C#K#E#D#C#J#E#D#C#I#E#D#C#H#E#D#C#G#E#D#C#F#E#D#C#.0B0A");//d + "\n" +

                string d = Regex.Replace(s, @"((?<=-|^)[^1-9]*)|((?'z'0)[0A-E]*((?=[1-9])|(?'-z'(?=[F-L\.]|$))))|((?'b'[F-L])(?'z'0)[0A-L]*((?=[1-9])|(?'-z'(?=[\.]|$))))", "${b}${z}");

                return Regex.Replace(d, ".", delegate(Match m) { return "负 空〇一二三四五六七八九空空空空空空空空空十百千万亿兆京垓秭穰"[m.Value[0] - '-'].ToString(); });
            }
            catch
            {
                return "";
            }
        }
        /// <summary>
        /// 将整形转换成中写汉字
        /// </summary>
        /// <param name="strNumber">数字的字符串形式</param>
        /// <returns></returns>
        public static string ChangeToMiddle(string strNumber)
        {
            try
            {
                return ChangeToMiddle(Convert.ToInt32(strNumber));
            }
            catch
            {
                return "";
            }
        }


        public static string Convert2Digit(string str)
        {
            char c1 = str[0];
            char c2 = str[1];
            string s2 = c2 == '0' ? "" : new string(ConvertChinese(c2), 1);
            string rstring = "";
            if (c1 == '0')
            {
                rstring = s2;
            }
            else
            {
                rstring = ConvertChinese(c1) + "十" + s2;
            }
            return rstring;
        }
        public static char ConvertChinese(char c)
        {
            //"零壹贰叁肆伍陆柒捌玖拾"   
            char cc = '\0';
            switch (c)
            {
                case '0': cc = '〇'; break;
                case '1': cc = '一'; break;
                case '2': cc = '二'; break;
                case '3': cc = '三'; break;
                case '4': cc = '四'; break;
                case '5': cc = '五'; break;
                case '6': cc = '六'; break;
                case '7': cc = '七'; break;
                case '8': cc = '八'; break;
                case '9': cc = '九'; break;
            }
            return (cc);
        }
        public static string GetDateStr(DateTime dt)
        {
            string y = dt.Year.ToString();
            char[] arry = y.ToCharArray();
            y = string.Empty;
            foreach (char c in arry)
            {
                y += ConvertChinese(c);
            }
            string M = Convert2Digit(dt.Month.ToString().PadLeft(2, '0'));
            string d = Convert2Digit(dt.Day.ToString().PadLeft(2, '0'));
            return y + "年" + M + "月" + d + "日";
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strMoneyCN"></param>
        /// <returns></returns>
        public static double ConvertMoneyToNum(string strMoneyCN)
        {
            ///万以上部分
            string strMoneyWanUp = "";

            ///万以下部分
            string strMoneyWanDown = "";

            if (strMoneyCN.LastIndexOf("万") != -1)
            {
                strMoneyWanUp = strMoneyCN.Substring(0, strMoneyCN.LastIndexOf("万"));

                strMoneyWanDown = strMoneyCN.Substring(strMoneyCN.LastIndexOf("万") + 1);

                return GetNumberFormCNStrUnless10W(strMoneyWanUp) * 10000 + GetNumberFormCNStrUnless10W(strMoneyWanDown);
            }
            else
            {
                return GetNumberFormCNStrUnless10W(strMoneyCN);
            }
        }
        /// <summary>
        /// 获取不大于十万的数字
        /// </summary>
        /// <param name="strMoney"></param>
        /// <returns></returns>
        public static double GetNumberFormCNStrUnless10W(string strMoney)
        {
            int iWan = 0;

            string strWan = "";

            int iQian = 0;

            string strQian = "";

            int iBai = 0;

            string strBai = "";

            int iShi = 0;

            string strShi = "";

            int iGe = 0;

            string strGe = "";

            int iGeIndex = -1;

            int iGeIndex2 = -1;


            if (strMoney.IndexOf("万") != -1)
            {
                iGeIndex = strMoney.IndexOf("万");

                iGeIndex2 = iGeIndex;

                strWan = strMoney.Substring(0, iGeIndex);

                strWan = strWan.Replace("零", "");

                iWan = GetNumberFromCNString(strWan);
            }
            if (strMoney.IndexOf("仟") != -1 || strMoney.IndexOf("千")!=-1)
            {
                iGeIndex2 = iGeIndex;

                if (strMoney.IndexOf("仟") != -1)
                {
                    iGeIndex = strMoney.IndexOf("仟");
                }
                else
                {
                    iGeIndex = strMoney.IndexOf("千");
                }

                strQian = strMoney.Substring(iGeIndex2 + 1, iGeIndex - iGeIndex2 - 1);

                strQian = strQian.Replace("零", "");

                iQian = GetNumberFromCNString(strQian);
            }
            if (strMoney.IndexOf("佰") != -1 || strMoney.IndexOf("百")!=-1)
            {
                iGeIndex2 = iGeIndex;

                if (strMoney.IndexOf("佰") != -1)
                {
                    iGeIndex = strMoney.IndexOf("佰");
                }
                else
                {
                    iGeIndex = strMoney.IndexOf("百");
                }
                strBai = strMoney.Substring(iGeIndex2 + 1, iGeIndex - iGeIndex2 - 1);

                strBai = strBai.Replace("零", "");

                iBai = GetNumberFromCNString(strBai);
            }
            if (strMoney.IndexOf("拾") != -1 || strMoney.IndexOf("十")!=-1)
            {
                iGeIndex2 = iGeIndex;

                if (strMoney.IndexOf("拾") != -1)
                {

                    iGeIndex = strMoney.IndexOf("拾");
                }
                else
                {
                    iGeIndex = strMoney.IndexOf("十");
                }

                strShi = strMoney.Substring(iGeIndex2 + 1, iGeIndex - iGeIndex2 - 1);

                strShi = strShi.Replace("零", "");

                iShi = GetNumberFromCNString(strShi);
            }
            if (strMoney.IndexOf("圆") != -1 || strMoney.IndexOf("元") != -1)
            {
                iGeIndex2 = iGeIndex;

                if (strMoney.IndexOf("圆") != -1)
                {
                    iGeIndex = strMoney.IndexOf("圆");
                }
                else
                {
                    iGeIndex = strMoney.IndexOf("元");
                }
                strGe = strMoney.Substring(iGeIndex2 + 1, iGeIndex - iGeIndex2 - 1);

                strGe = strGe.Replace("零", "");

                iGe = GetNumberFromCNString(strGe);
            }
            else
            {
                iGeIndex2 = iGeIndex;

                strGe = strMoney.Substring(iGeIndex + 1, strMoney.Length - iGeIndex - 1);

                strGe = strGe.Replace("零", "");

                iGe = GetNumberFromCNString(strGe);
            }
            return Convert.ToDouble(iWan * 10000 + iQian * 1000 + iBai * 100 + iShi * 10 + iGe);
        }
        public static int GetNumberFromCNString(string strCNString)
        {
            switch (strCNString)
            {
                case "零":
                    return 0;
                case "壹":
                    return 1;
                case "贰":
                    return 2;
                case "叁":
                    return 3;
                case "肆":
                    return 4;
                case "伍":
                    return 5;
                case "陆":
                    return 6;
                case "柒":
                    return 7;
                case "捌":
                    return 8;
                case "玖":
                    return 9;
                case "":
                    return 0;
                case "一":
                    return 1;
                case "二":
                    return 2;
                case "三":
                    return 3;
                case "四":
                    return 4;
                case "五":
                    return 5;
                case "六":
                    return 6;
                case "七":
                    return 7;
                case "八":
                    return 8;
                case "九":
                    return 9;
                default:
                    throw new Exception("未能识别的大写金额字符：" + strCNString + "！");
            }
        }
        
    }
}
